/**
 * 合同管理-审批列表控制器
 */

import $_fn from '~plugins'
export default ['$scope','$rootScope','$location','$ui','http','$state',function($scope,$rootScope,$location,$ui,http,$state) {
  $scope.err = {}
  //url参数获取
  $scope.search = !$_fn.isEmptyObject($location.search())?$location.search():{state:1}
  $scope.params = $scope.search
  console.log('$scope.search',$scope.search)
  $scope.$_fn = $_fn
  // $scope.ch_id_all = $_fn.ch_id_all
  //警告提示
  $scope.errorToast = function(text) {
    $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
  }
  //页面操作项分类
  $scope.state = $scope.search.state || 1
  $scope.dataList = []
  //分页参数
  $scope.page = {
    current: $scope.search.pageNum ||  1
  }
  //获取数据的入参
  $scope.params.s_id =  2
  $scope.params.sIds =  '3'

  //待签合同列表
  let formData = {}
  $scope.url = 'selectOrderList.do'
  if($scope.state == 1){
    //待签合同列表
    $scope.url = 'selectOrderList.do'
  }else if ($scope.state == 2){
    //历史合同列表
    $scope.url = 'selectHistoryContract.do'
  }else if($scope.state == 3){
    //合同编号
    $scope.url = 'selectContractNoList.do'
  }
  //获取列表数据的函数
  $scope.getDada = function(page,params){
    params.pageNum = page
    http.post($scope.url, params, function (data) {
      if(data.errcode == 1){
        $scope.$apply(function(){
          let _data = data.data
          //分页所需数据
          $scope.page.pageNum =  $scope.page.current
          $scope.page.pageSize = _data.pageSize
          $scope.page.total = _data.total
          //列表数据
          $scope.dataList = _data.list
          console.log('ryy-datlist:', _data, $scope.dataList)
        })
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
      }
    },function(){
      $scope.dataList = []
    })
  }
  //初始化数据
  $scope.getDada(1, $scope.params)

  /**
   * 四级权限菜单
   * state = 1                      地址栏所要显示的菜单
   * path = link@、alert@           link(连接)、alert(弹窗) 例:link@application.intoa(跳转到全部进件列表)
   * */
  $scope.operation = function (path,item) {
    $rootScope.refreshPage = $scope.getDada
    var _path = path.split('@')
    let params = JSON.stringify(item)
    console.log("-------",item)
    console.log('_path:', _path)
    if(_path[0] == 'link'){
        console.log(path)
       $state.go(_path[1],{state:$scope.state,items:params,pid:item.pid,oid:item.oid,s_id:item.s_id})
       //$state.go(_path[1],item)
    }
    if(_path[0] == 'alert'){
        $ui.modal({content:"<"+_path[1]+" params="+params+" ></"+_path[1]+">"})
    }
  }
  //搜索
  $scope.searchHandle = function () {
    if($scope.params.keyword && $scope.params.type==-1){
      $scope.err.type = true
      $scope.errorToast('左侧字段没有选择')
      return
    }
    $location.search($scope.params)
    $scope.getDada(1,$scope.params)
  }

  //获取当前路由的四级权限
  let menu = $_fn.getLocal('parentMenu')
  $scope.subMenu = []
  let _subMenu = function (path,_menu) {
    console.log('menu-local:', _menu, path)
    for (let a in _menu) {
      if (_menu[a].children) {
        for (let i in _menu[a].children) {
          if (_menu[a].children[i].path == path) {
            $scope.subMenu = _menu[a].children[i].children
            break
            // $scope.subMenu = $scope.subMenu.concat(_menu[a].children[i].children)
            // // break
          } else {
            console.log('menu-else:', _menu[a].children[i].path, _menu[a].children[i].children)
            _subMenu(path,_menu[a].children[i].children)
          }
        }
      }
    }
  }
  _subMenu('/application/contract',menu.children)

  $scope.$watch('subMenu',function(newVal){
    console.log('watch-menu:', newVal)
  },true)
}]